angular.module('testData', [])
    .factory('testData', [
        '$q', '$http', '$timeout',
        function($q, $http, $timeout) {
            var compare, routine, sources;
            sources = void 0;
            $http.get('json/testdata.json').success(function(res) {
                sources = res;
            });
            compare = function(data, params) {
                var p, q;
                q = $q.defer();
                for (p in params) {
                    angular.forEach(data, function(v, k) {
                        var pa, result_number;
                        if (p === v.en_name || p === 'LDHL' && v.en_name === 'LDH-L') {
                            result_number = v.unit === '%' ? params[p] * 100 : params[p];
                            v.number = result_number;
                            if (params[p]) {
                                pa = parseFloat(params[p]);
                                if (pa <= v.min_value) {
                                    v.status = '偏低';
                                } else {
                                    v.status = pa >= v.max_value ? '偏高' : '正常';
                                }
                            }
                        }
                        q.resolve(data);
                    });
                }
                return q.promise;
            };
            routine = function(params) {
                var arraies, q;
                q = $q.defer();
                arraies = [];
                $timeout((function() {
                    var v;
                    for (v in sources) {
                        if (v === params.alia) {
                            angular.forEach(sources[v], function(a) {
                                if (a.gender.toString() === params.gender.toString()) {
                                    arraies.push(a);
                                }
                            });
                        }
                    }
                    q.resolve(compare(arraies, params));
                }), 100);
                return q.promise;
            };
            return {
                routine: routine
            };
        }
    ]);
